home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / library / select.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  8KB  |  261 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  select.c,v 1.1.1.1 1994/04/04 04:30:33 amiga Exp
  20.  *
  21.  *  select.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:33  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.2  1993/11/05  22:00:59  mwild
  26.  *  extensively rewritten to work better along with inet.library
  27.  *
  28.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  29.  *  Initial revision
  30.  *
  31.  */
  32.  
  33. #define KERNEL
  34. #include "ixemul.h"
  35. #include "kprintf.h"
  36.  
  37. #include <sys/time.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40.  
  41. #include "select.h"
  42.  
  43. #define __time_req (u.u_time_req)
  44. #define __tport    (u.u_sync_mp)
  45.  
  46. static void inline setcopy(int nfd, u_int *ifd, u_int *ofd)
  47. {
  48.   /* this procedure is here, because it's "normal" that if you only
  49.    * want to select on fd 0,1,2 eg. you only pass a long to select,
  50.    * not a whole fd_set, so we can't simply copy over results in the
  51.    * full size of an fd_set.. */
  52.   
  53.   /* we have to copy that many longs... */
  54.   nfd = (nfd+31) >> 5;
  55.   while (nfd--) *ofd++ = *ifd++;
  56. }
  57.  
  58. int
  59. select(int nfd, fd_set *ifd, fd_set *ofd, fd_set *efd, struct timeval *timeout)
  60. {
  61.   struct file *f;
  62.   int i, waitin, waitout, waitexc, dotout;
  63.   int result, ostat;
  64.   u_int wait_sigs;
  65.   struct timeval end_time;
  66.   int skipped_wait;
  67.   struct user *p = &u;
  68.   u_long recv_wait_sigs = 0;
  69.  
  70.   /* as long as I don't support anything similar to a network, I surely
  71.    * won't get any exceptional conditions, so *efd is mapped into 
  72.    * *ifd, if it's set. */
  73.    
  74.   /* first check, that all included descriptors are valid and support
  75.    * the requested operation. If the user included a request to wait
  76.    * for a descriptor to be ready to read, while the descriptor was only
  77.    * opened for writing, the requested bit is immediately cleared
  78.    */
  79.   waitin = waitout = waitexc = 0;
  80.   if (nfd > NOFILE) nfd = NOFILE;
  81.  
  82.   for (i = 0; i < nfd; i++)
  83.     {
  84.       if (ifd && FD_ISSET(i, ifd) && (f = p->u_ofile[i]))
  85.     {
  86.       if (!f->f_read || !f->f_select)
  87.         FD_CLR(i, ifd);
  88.       else
  89.         ++waitin;
  90.     }
  91.       if (ofd && FD_ISSET(i, ofd) && (f = p->u_ofile[i]))
  92.     {
  93.       if (!f->f_write || !f->f_select)
  94.         FD_CLR(i, ofd);
  95.       else
  96.         ++waitout;
  97.     }
  98.       if (efd && FD_ISSET(i, efd) && (f = p->u_ofile[i]))
  99.     {
  100.       /* question: can an exceptional condition also occur on a 
  101.        * write-only fd?? */
  102.       if (!f->f_read || !f->f_select)
  103.         FD_CLR(i, efd);
  104.       else
  105.         ++waitexc;
  106.     }
  107.     }
  108.  
  109.   /* now, if we see, that we're not waiting for anything, AND if
  110.    * timeout is NULL (not 0 seconds!), than select() is just something
  111.    * like a very complex pause() call ;-)) */
  112.   if (waitin + waitout + waitexc == 0 && !timeout)
  113.     return syscall (SYS_pause);
  114.  
  115.   if ((dotout = (timeout && timerisset(timeout))))
  116.     {
  117.       /* remember the time we have to leave (timeout) */
  118.       syscall(SYS_gettimeofday, &end_time, 0);
  119.       end_time.tv_usec += timeout->tv_usec;
  120.       /* this conversion should be cheaper than a division and a modulo.. */
  121.       while (end_time.tv_usec >= 1000000)
  122.         {
  123.       end_time.tv_usec -= 1000000;
  124.       end_time.tv_sec++;
  125.     }
  126.       end_time.tv_sec += timeout->tv_sec;
  127.     }
  128.  
  129.   /* have to make sure we can clean up the timer-request ! */
  130.   ostat = p->p_stat;
  131.   p->p_stat = SSLEEP;
  132.   p->p_wchan = (caddr_t) select; /* will once be an own variable */
  133.   p->p_wmesg = "select";
  134.  
  135.  
  136.   for (skipped_wait = 0; ; skipped_wait=1)
  137.     {
  138.       fd_set readyin, readyout, readyexc;
  139.       int tout, readydesc, cmd;
  140.  
  141.       FD_ZERO(&readyin);
  142.       FD_ZERO(&readyout);
  143.       FD_ZERO(&readyexc);
  144.  
  145.       tout = 
  146.         readydesc = 0;
  147.  
  148.       /* have to always wait for the `traditional' ^C and the library internal
  149.        * sleep_sig as well */
  150.       wait_sigs = SIGBREAKF_CTRL_C | (1 << p->u_sleep_sig);
  151.  
  152.       if (/*(dotout || !timeout) &&*/ skipped_wait)
  153.     {
  154.       cmd = SELCMD_CHECK;
  155.  
  156.       /* have all watched files get prepared for selecting */
  157.           for (i = 0; i < nfd; i++)
  158.         {
  159.           if (ifd && FD_ISSET (i, ifd) && (f = p->u_ofile[i]))
  160.             wait_sigs |= f->f_select (f, SELCMD_PREPARE, SELMODE_IN);
  161.           if (ofd && FD_ISSET (i, ofd) && (f = p->u_ofile[i]))
  162.             wait_sigs |= f->f_select (f, SELCMD_PREPARE, SELMODE_OUT);
  163.           if (efd && FD_ISSET (i, efd) && (f = p->u_ofile[i]))
  164.             wait_sigs |= f->f_select (f, SELCMD_PREPARE, SELMODE_EXC);
  165.         }
  166.  
  167.       /* note: we never post a timeout request of less than SELTIMEOUT, so
  168.            * select() precision depends on SELTIMEOUT. However, considering that
  169.            * this call emulates a Unix syscall, this comes quite near to Unix
  170.            * precision, if not better ;-)) */
  171.       __time_req->tr_time.tv_sec = 0;
  172.       __time_req->tr_time.tv_usec = SELTIMEOUT;
  173.           __time_req->tr_node.io_Command = TR_ADDREQUEST;
  174.           SendIO((struct IORequest *)__time_req);
  175.           /* clear the bit, it's used for sync packets too, and might be set */
  176.           SetSignal (0, 1 << __tport->mp_SigBit);
  177.       wait_sigs |= 1 << __tport->mp_SigBit;
  178.  
  179.           /* now wait for all legally possible signals, this includes BSD
  180.            * signals (but want at least one signal set!) */
  181.           while (! (recv_wait_sigs = Wait (wait_sigs))) ;
  182.  
  183.       /* IMPORTANT: unqueue the timer request BEFORE polling the fd's,
  184.        *            or __wait_packet() will treat the timer request
  185.        *            as a packet... */
  186.  
  187.       if (! CheckIO ((struct IORequest *)__time_req))
  188.             AbortIO ((struct IORequest *)__time_req);
  189.           WaitIO ((struct IORequest *)__time_req);
  190.         }
  191.       else
  192.     cmd = SELCMD_POLL;
  193.  
  194.       /* no matter what caused Wait() to return, wait for all requests to
  195.        * complete (we CAN'T abort a DOS packet, sigh..) */
  196.  
  197.       /* collect information from the file descriptors */
  198.       for (i = 0; i < nfd; i++)
  199.     {
  200.       if (ifd && FD_ISSET (i, ifd) && (f = p->u_ofile[i])
  201.           && f->f_select (f, cmd, SELMODE_IN))
  202.         {
  203.           FD_SET (i, &readyin);
  204.           ++ readydesc;
  205.         }
  206.       if (ofd && FD_ISSET (i, ofd) && (f = p->u_ofile[i])
  207.           && f->f_select (f, cmd, SELMODE_OUT))
  208.         {
  209.           FD_SET (i, &readyout);
  210.           ++ readydesc;
  211.         }
  212.       if (efd && FD_ISSET (i, efd) && (f = p->u_ofile[i])
  213.               && f->f_select (f, cmd, SELMODE_EXC))
  214.         {
  215.           FD_SET (i, &readyexc);
  216.           ++ readydesc;
  217.         }
  218.     }
  219.  
  220.       /* we have a timeout condition, if readydesc == 0, dotout == 1 and 
  221.        * end_time < current time */
  222.       if (!readydesc && dotout)
  223.         {
  224.           struct timeval current_time;
  225.           
  226.           syscall(SYS_gettimeofday, ¤t_time, 0);
  227.           tout = timercmp (&end_time, ¤t_time, <);
  228.     }
  229.  
  230.       if (readydesc || tout || (timeout && !timerisset(timeout)))
  231.     {
  232.       if (ifd) setcopy(nfd, (u_int *)&readyin,  (u_int *)ifd);
  233.       if (ofd) setcopy(nfd, (u_int *)&readyout, (u_int *)ofd);
  234.       if (efd) setcopy(nfd, (u_int *)&readyexc, (u_int *)efd);
  235.       result = readydesc; /* ok for tout, since then readydesc is already 0 */
  236.       break;
  237.     }
  238.  
  239.       if (recv_wait_sigs & (SIGBREAKF_CTRL_C | (1 << p->u_sleep_sig)))
  240.         {
  241.           result = -1;
  242.           break;
  243.         }
  244.     }
  245.  
  246.   p->p_wchan = 0;
  247.   p->p_wmesg = 0;
  248.   p->p_stat = ostat;
  249.   /* need special processing for ^C here, as that is completely disabled
  250.      when we're SSLEEPing */
  251.   if (recv_wait_sigs & SIGBREAKF_CTRL_C)
  252.     _psignal (FindTask (0), SIGINT);
  253.   setrun (FindTask (0));
  254.  
  255.   if (result == -1)
  256.     /* have to set this here, since errno can be changed in signal handlers */
  257.     *(p->u_errno) = EINTR;
  258.  
  259.   return result;
  260. }
  261.